package com.yxuan.springboot.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxuan.springboot.model.FocusInfomation;
import com.yxuan.springboot.model.Information;
import com.yxuan.springboot.model.UserLogin;
import com.yxuan.springboot.service.FocusInfomationService;
import com.yxuan.springboot.service.UserLoginService;
import com.yxuan.springboot.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * @author zchao
 *   FocusInfomation控制器
 * @date 2023-06-12 16:39:47
 */
@Controller
@Api(tags = "文章关注表")
@RequestMapping(value = "/focusinfomation", method = {RequestMethod.POST,RequestMethod.GET})
public class FocusInfomationController extends BaseController<FocusInfomation>{

    @Autowired
    private FocusInfomationService focusInfomationService;

    @Autowired
    private UserLoginService userLoginService;

    /**
     *  添加关注院校
     */
    @ApiOperation(value = "添加关注文章", notes = "添加关注文章")
    @RequestMapping("/addFocusInformation")
    @ResponseBody
    public String addFocusInformation(HttpServletRequest request, Long informationID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        Integer userID = userLogin1.getId();

        FocusInfomation focusInfomation = new FocusInfomation();
        focusInfomation.setInformationId(informationID);
        focusInfomation.setUserId(userID);
        focusInfomationService.save(focusInfomation);
        return SUCCESS;
    }

    @ApiOperation(value = "获取关注文章列表", notes = "获取关注文章列表")
    @RequestMapping("/loadListByUserID")
    @ResponseBody
    public List<Information> loadListByUserID(HttpServletRequest request, Long informationID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        Integer userID = userLogin1.getId();
        return focusInfomationService.getListByUserID(userID);
    }

    @ApiOperation(value = "取消关注", notes = "取消关注")
    @RequestMapping("/cancelFocusByUserID")
    @ResponseBody
    public String cancelFocusByUserID(HttpServletRequest request,Long informationID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        FocusInfomation focusInfomation = new FocusInfomation();
        Integer userID = userLogin1.getId();

        focusInfomation.setUserId(userID);
        focusInfomation.setInformationId(informationID);
        focusInfomationService.remove(focusInfomation);
        return SUCCESS;
    }

    @ApiOperation(value = "是否被关注", notes = "是否被关注")
    @RequestMapping("/isFocused")
    @ResponseBody
    public FocusInfomation isFocused(HttpServletRequest request, Long informationID) throws Exception {
        String token = request.getHeader("token");
        //解析token
        Claims claims = JwtUtil.parseJWT(token);
        String sub = claims.get("sub", String.class);
        JsonNode subNode = new ObjectMapper().readTree(sub);
        String phoneNumber = subNode.get("phoneNumber").asText();

        UserLogin userLogin = new UserLogin();
        userLogin.setPhoneNumber(phoneNumber);
        UserLogin userLogin1 = userLoginService.findObjectByPramas(userLogin);
        FocusInfomation focusInfomation = new FocusInfomation();
        Integer userID = userLogin1.getId();
        focusInfomation.setUserId(userID);
        focusInfomation.setInformationId(informationID);
        return focusInfomationService.findObjectByPramas(focusInfomation);
    }
}
